﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;


public partial class Admin1_Default2 : System.Web.UI.Page
{
    Datacon dataconn = new Datacon();

    public void ReflashGrid()
    {
        dataconn.bind(GridView1, "select * from [USER] where type = 'CUS' order by ID DESC");
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            dataconn.bindinfostring(GridView1, "select * from [USER] where type='CUS' order by ID DESC", "ID");

        }

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (TextBox1.Text == "")
        {
            dataconn.bindinfostring(GridView1, "select * from [USER] where type='CUS' order by ID DESC", "ID");
        }
        else
        {
            dataconn.bind(GridView1, "select * from [USER] where type='CUS' and  " + DropDownList1.SelectedValue + "   Like'%" + TextBox1.Text + "%'");
        }

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        dataconn.eccom("delete from [USER] where type='CUS' and ID='" + GridView1.DataKeys[e.RowIndex].Value + "'");
        Response.Redirect("ManageStudent.aspx");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        ReflashGrid();
    }

    protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {

            CheckBox ch = (CheckBox)gr.FindControl("CheckBox1");
            if (ch.Checked == true)
            {

                ch.Checked = false;

            }
            else
            {

                ch.Checked = true;

            }

        }
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {

            CheckBox ch = (CheckBox)gr.FindControl("CheckBox1");
            ch.Checked = false;

        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        ReflashGrid();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SqlConnection cn = dataconn.getcon();
        cn.Open();
        string ID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        string USER_NAME = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
        string LOGIN_NAME = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
        string LOGIN_PASS = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();
        string COMPANY = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString();
        string DEPARTMENT = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();
        //string PAPERID = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString();
        SqlDataReader dr;
        string sql = string.Format("UPDATE [USER] SET [USER_NAME]='{0}',LOGIN_NAME='{1}',LOGIN_PASS='{2}',COMPANY='{3}',DEPARTMENT='{4}' where id='{5}'", USER_NAME, LOGIN_NAME, LOGIN_PASS, COMPANY, DEPARTMENT,ID);
        SqlCommand myCommand = new SqlCommand(sql, cn);
        dr = myCommand.ExecuteReader();
        GridView1.EditIndex = -1;
        cn.Close();
        ReflashGrid();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        ReflashGrid();
    }

}